/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.button;

import java.util.ArrayList;
import java.util.List;

import org.rails.extjs.Component;
import org.rails.extjs.Option;
import org.rails.extjs.javascript.Function;
import org.rails.extjs.menu.CheckItem;

public class CycleButton extends SplitButton {
	
	protected List<CheckItem> items = new ArrayList<CheckItem>();

	public CycleButton() {
		super();
	}

	public CycleButton(List<CheckItem> items) {
		super();
		this.items = items;
	}

	public CycleButton(Option configOption) {
		super(configOption);
	}
	
	@Override
	public void createItems(){
		super.createItems();
		if(items.size() > 0)
			addOption("items",items);
	}
	
	public void insertItem(int index,CheckItem item){
		items.add(index,item);		
	}
	
	public void addItem(CheckItem item){
		items.add(item);		
	}
	
	public List<CheckItem> getItems(){
		return items;
	}
	
	public void removeItem(int index){
		items.remove(index);
	}
	
	public Component getItem(int index){
		return items.get(index);
	}
	
	public void clearItems(){
		items.clear();
	}
	
	public void setChangeHandler(Function changeHandler){
		addOption("changeHandler", changeHandler);
	}
	
	public Function getChangeHandler(){
		return (Function)getOption("changeHandler");
	}
	
	public void setForceIcon(String forceIcon){
		addOption("forceIcon", forceIcon);
	}
	
	public String getForceIcon(){
		return getString("forceIcon","");
	}
	
	public void setPrependText(String prependText){
		addOption("prependText", prependText);
	}
	

	public String getPrependText(){
		return getString("prependText","");
	}
	
	public void setShowText(Boolean showText){
		addOption("showText", showText);
	}
	
	public Boolean isShowText(){
		return getBoolean("showText",false);
	}
	
	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.CycleButton(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}

	
}
